package org.nmgyjt.springboot.controller;

import lombok.extern.slf4j.Slf4j;
import org.nmgyjt.springboot.beans.admin.CsSystemUserCreateBean;
import org.nmgyjt.springboot.entity.ApiRes;
import org.nmgyjt.springboot.entity.admin.UserInfo;
import org.nmgyjt.springboot.entity.exception.BizException;
import org.nmgyjt.springboot.entity.refuge.RefugeInfo;
import org.nmgyjt.springboot.entity.sms.YuFuMSSendEntity;
import org.nmgyjt.springboot.service.ICsSystemUserService;
import org.nmgyjt.springboot.service.RefugeService;
import org.nmgyjt.springboot.service.impl.SendSMSServiceImpl;
import org.nmgyjt.springboot.utils.IDAASUserUtils;
import org.nmgyjt.springboot.utils.ZWWXUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cs/refuge")
@Slf4j
public class RefugeController {

    @Autowired
    private RefugeService refugeService;
    @Autowired
    SendSMSServiceImpl smsService;
    @Autowired
    ZWWXUtils zwwxUtils;
    @Autowired
    ICsSystemUserService iCsSystemUserService;

    @GetMapping("/getById")
    public ApiRes getById(Long id) {
        return ApiRes.ok(refugeService.getById(id));
    }

    @GetMapping("/getByDepId")
    public ApiRes getByDepId(String depId) {
        return ApiRes.ok(refugeService.getByDepId(depId));
    }

    @PostMapping("/saveOrUpdate")
    public ApiRes saveOrUpdate(@RequestBody RefugeInfo refugeInfo) {
        boolean first = false;
        boolean dep = false;
        boolean org = false;
        if(!ObjectUtils.isEmpty(refugeInfo.getId())){
            RefugeInfo refugeInfo1=refugeService.getById(refugeInfo.getId());
            if(ObjectUtils.isEmpty(refugeInfo1.getHeadPhone())){
                first = true;
            }else{
                if(!refugeInfo1.getHeadPhone().equals(refugeInfo.getHeadPhone())){
                    first = true;
                }
            }
            if (ObjectUtils.isEmpty(refugeInfo1.getDepHeadPhone())) {
                dep = true;
            } else {
                if (!refugeInfo1.getDepHeadPhone().equals(refugeInfo1.getDepHeadPhone())) {
                    dep = true;
                }
            }
            if (ObjectUtils.isEmpty(refugeInfo1.getOrgHeadPhone())) {
                org = true;
            } else {
                if (!refugeInfo1.getOrgHeadPhone().equals(refugeInfo1.getOrgHeadPhone())) {
                    org = true;
                }
            }
        }
        refugeInfo.setAreaCode(null);
        refugeInfo.setState("0");
        boolean f=refugeService.saveOrUpdate(refugeInfo);
        if(f){
            YuFuMSSendEntity sendEntity = new YuFuMSSendEntity();
            List<String> params = new ArrayList<>();
            List<String> phone = new ArrayList<>();
            String ssoAccessToken = zwwxUtils.getGovWxToken();
            if(first){
                CsSystemUserCreateBean bean = new CsSystemUserCreateBean();
                bean.setCreatedBy(refugeInfo.getCreatedBy());
                bean.setOrgID(refugeInfo.getDepId());
                bean.setMenuID("1000000012");
                bean.setOrgName(refugeInfo.getRefugeName());
                bean.setJob(refugeInfo.getFirstJob());
                bean.setName(refugeInfo.getHeadName());
                bean.setPhone(refugeInfo.getHeadPhone());
                try {
                    UserInfo info = IDAASUserUtils.selectUserInfoFromIDAASByOpenIdV2(bean.getPhone());
                    if (null == info) {
                        info = iCsSystemUserService.createCsSystemUser(bean);
                        log.info("------------------创建用户信息：{}", info);
                    }
                    Integer code=zwwxUtils.getZWUserInfo(refugeInfo.getHeadPhone(),"refuge-detail/index?depId="+refugeInfo.getDepId(),ssoAccessToken);
                    refugeInfo.setSnycState(code.toString());
                    phone.add("+86"+refugeInfo.getHeadPhone());
                    params.add(refugeInfo.getHeadName());
                    params.add("避难场所"+refugeInfo.getRefugeName());
                    sendEntity.setTemplateParamSet(params);
                    sendEntity.setPhoneNumSet(phone);
                    sendEntity.setType("notification");
                    smsService.sendMS(sendEntity);
                } catch (Exception e) {
                    throw new BizException("创建统一认证用户失败,请联系系统管理员!");
                }

            }
            if (dep) {
                CsSystemUserCreateBean bean = new CsSystemUserCreateBean();
                bean.setCreatedBy(refugeInfo.getCreatedBy());
                bean.setOrgID(refugeInfo.getDepId());
                bean.setMenuID("1000000012");
                bean.setOrgName(refugeInfo.getRefugeName());
                bean.setJob(refugeInfo.getDepJob());
                bean.setName(refugeInfo.getDepHead());
                bean.setPhone(refugeInfo.getDepHeadPhone());
                try {
                    UserInfo info = IDAASUserUtils.selectUserInfoFromIDAASByOpenIdV2(bean.getPhone());
                    if (null == info) {
                        info = iCsSystemUserService.createCsSystemUser(bean);
                        log.info("------------------创建用户信息：{}", info);
                    }
                    Integer code2 = zwwxUtils.getZWUserInfo(refugeInfo.getDepHeadPhone(), "refuge-detail/index?depId="+refugeInfo.getDepId(),ssoAccessToken);
                    refugeInfo.setDepSnycState(code2.toString());
                    phone.clear();
                    params.clear();
                    phone.add("+86" + refugeInfo.getDepHeadPhone());
                    params.add(refugeInfo.getDepHead());
                    params.add("避难场所"+refugeInfo.getRefugeName());
                    sendEntity.setTemplateParamSet(params);
                    sendEntity.setPhoneNumSet(phone);
                    sendEntity.setType("notification");
                    smsService.sendMS(sendEntity);
                } catch (Exception e) {
                    throw new BizException("创建统一认证用户失败,请联系系统管理员!");
                }

            }
            if (org) {
                CsSystemUserCreateBean bean = new CsSystemUserCreateBean();
                bean.setCreatedBy(refugeInfo.getCreatedBy());
                bean.setOrgID(refugeInfo.getDepId());
                bean.setMenuID("1000000012");
                bean.setOrgName(refugeInfo.getRefugeName());
                bean.setJob(refugeInfo.getOrgJob());
                bean.setName(refugeInfo.getOrgHead());
                bean.setPhone(refugeInfo.getOrgHeadPhone());
                try {
                    UserInfo info = IDAASUserUtils.selectUserInfoFromIDAASByOpenIdV2(bean.getPhone());
                    if (null == info) {
                        info = iCsSystemUserService.createCsSystemUser(bean);
                        log.info("------------------创建用户信息：{}", info);
                    }
                    Integer code3 = zwwxUtils.getZWUserInfo(refugeInfo.getOrgHeadPhone(), "refuge-detail/index?depId="+refugeInfo.getDepId(),ssoAccessToken);
                    refugeInfo.setOrgSnycState(code3.toString());
                    phone.clear();
                    params.clear();
                    phone.add("+86" + refugeInfo.getOrgHeadPhone());
                    params.add(refugeInfo.getOrgHead());
                    params.add("避难场所"+refugeInfo.getRefugeName());
                    sendEntity.setTemplateParamSet(params);
                    sendEntity.setPhoneNumSet(phone);
                    sendEntity.setType("notification");
                    smsService.sendMS(sendEntity);
                } catch (Exception e) {
                    throw new BizException("创建统一认证用户失败,请联系系统管理员!");
                }

            }
            refugeService.saveOrUpdate(refugeInfo);
            return ApiRes.ok(refugeInfo.getId());
        }else{
            return ApiRes.customFail("保存失败！");
        }
    }

    @GetMapping("/list")
    public ApiRes list() {
        return ApiRes.ok(refugeService.list());
    }

    @GetMapping("/deleteByDepId")
    public ApiRes deleteByDepId(String depId) {
        Boolean b=refugeService.deleteByDepId(depId);
        if(b){
            return ApiRes.ok();
        }else{
            return ApiRes.customFail("删除失败");
        }

    }
}
